package com.lbssoft.gpsbd.service.impl;

import com.lbssoft.gpsbd.dao.PermissionMapper;
import com.lbssoft.gpsbd.dao.RoleMapper;
import com.lbssoft.gpsbd.model.Permission;
import com.lbssoft.gpsbd.model.Result;
import com.lbssoft.gpsbd.model.Role;
import com.lbssoft.gpsbd.service.CacheService;
import com.lbssoft.gpsbd.service.PermissionService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * Created by nczc on 2018/2/24.
 */
@Service
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    @Lazy
    public PermissionMapper permissionMapper;

    @Autowired
    @Lazy
    public RoleMapper roleMapper;

    @Autowired
    public CacheService cacheService;



    @Override
    @Cacheable(value = "permis", key = "'user_'.concat(#uid.toString())")
    public List<Permission> getPermissionsByUid(long uid) {
        return permissionMapper.getPermissionsByUid(uid);
    }

    @Override
    @Cacheable(value = "permis", key = "'role_'.concat(#roleId.toString())")
    public List<Permission> getPermissionsByRoleId(int roleId) {
        return permissionMapper.getPermissionsByRoleId(roleId);
    }

    @Override
    public List<Permission> list(int groupId) {
        return permissionMapper.list(groupId);
    }

    @Override
    public void add(Permission permis) {
        //获取默认管理员角色信息
        Role role = roleMapper.getRoleByKey("root");

        //添加权限
        permissionMapper.add(permis);

        //修改排序
        permissionMapper.updateSort(permis.getId(),permis.getId());

        //强行给默认管理员角色授权
        roleMapper.addRolePermis(role.getId(),permis.getId());

        //清理角色资源缓存
        cacheService.cleanCacheAllRolePermiss();
    }

    @Override
    public void delete(int id) {
        permissionMapper.delete(id);
    }

    @Override
    public Set<Integer> getPermissionIds(int roleId,String types) {
        return permissionMapper.getPermissionIds(roleId,types);
    }

    @Override
    public void update(Permission permis) {
        permissionMapper.update(permis);
    }

    @Override
    public List<Permission> getPermissionsByPid(int parentId) {
        return permissionMapper.getPermissionsByPid(parentId);
    }

    @Override
    public List<Permission> getMenusByRoleId(@Param("roleId") int roleId,@Param("groupId") int groupId) {
        return permissionMapper.getMenusByRoleId(roleId,groupId);
    }

    @Override
    public Result sort(int id, int direction) {
        Result result  = new Result();
        List<Permission> list =  permissionMapper.getBrothersPermisById(id);
        //需要排序的权限索引位置
        int index = 0;
        //总公数据条数
        int total = list.size();
        int targetId=0;
        for(int i=0;i<total;i++){
            if(list.get(i).getId() == id){
                index = i;
                break;
            }
        }
        if(index==0&&direction==1){
            result.setCode(1);
            result.setMsg("已经是最上面");
            return result;
        }else if(index==total-1&&direction==0){
            result.setCode(1);
            result.setMsg("已经是最下面");
            return result;
        }

        if(direction==1){
            targetId =  list.get(index-1).getId();
        }else if(direction==0){
            targetId =  list.get(index+1).getId();
        }
        permissionMapper.displaceSort(id,targetId);
        return result;
    }
}
